﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.pictures
{
    public partial class edit : Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        public int channel_id;
        public Model.channel channel;
        public string imgurl = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                ShowParamError();
                return;
            }

            channel = new BLL.channel().GetModel(channel_id);

            if (!Page.IsPostBack)
            {
                CategoryTreeBind(0, "请选择所属类别……", this.channel_id, ddlcategory_id);

                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editChannel" + channel.id);
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addChannel" + channel.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.article model = new BLL.article().GetModel(_id);
            txtTitle.Text = model.title;
            ddlcategory_id.SelectedValue = model.category_id.ToString();
            txtSeoKeywords.Text = model.seo_keywords;
            txtSeoDescription.Text = model.seo_desc;
            txtContent.Value = model.content;
            txtLinkUrl.Text = model.link_url;
            chkRed.Checked = model.is_red == 1 ? true : false;
            chkHot.Checked = model.is_hot == 1 ? true : false;
            chkLock.Checked = model.is_lock == 1 ? true : false;
            txtClick.Text = model.click.ToString();
            txtSortId.Text = model.sort_id.ToString();

            string[] fileArr = model.img_urls.Split(',');
            txtImgUrl.Text = fileArr[0];

            imgurl = model.img_url;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.article model = new He.Model.article();
            BLL.article bll = new He.BLL.article();

            model.title = txtTitle.Text.Trim();

            if (channel.is_thumb == 1 && !string.IsNullOrEmpty(txtImgUrl.Text.Trim()))
            {
                model.img_url = "/uploadfiles/thumb/" + DateTime.Now.ToFileTime().ToString() + ".jpg";
                ImageHelper.CompressImage(txtImgUrl.Text.Trim(), model.img_url, channel.img_width, channel.img_height, "max");
            }
            else
            {
                model.img_url = txtImgUrl.Text.Trim();
            }

            model.img_urls = txtImgUrl.Text.Trim();
            model.price = 0;                                              //价格 
            model.category_id = int.Parse(ddlcategory_id.SelectedValue);
            model.content = txtContent.Value;
            model.link_url = txtLinkUrl.Text.Trim();
            model.click = int.Parse(txtClick.Text.Trim());
            model.sort_id = int.Parse(txtSortId.Text.Trim());

            model.seo_keywords = txtSeoKeywords.Text.Trim();
            //自动提取SEO描述
            if (txtSeoDescription.Text.Trim() != string.Empty)
            {
                model.seo_desc = txtSeoDescription.Text;
            }
            else
            {
                model.seo_desc = He.Common.StringHelper.ClearHtmlTags(txtContent.Value, 250);
            }
            model.is_top = 0;
            model.is_red = chkRed.Checked ? 1 : 0;
            model.is_hot = chkHot.Checked ? 1 : 0;
            model.is_lock = chkLock.Checked ? 1 : 0;
            model.add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            int aid = bll.Add(model);
            if (aid > 0)
            {
                SaveLogs(string.Format("[{0}管理]增加内容:{1}", channel.manage_title, model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.article bll = new BLL.article();
            Model.article model = new He.BLL.article().GetModel(id);
            model.title = txtTitle.Text.Trim();
            model.category_id = int.Parse(ddlcategory_id.SelectedValue);

            if (!string.IsNullOrEmpty(txtImgUrl.Text.Trim()))
            {
                if (channel.is_thumb == 1)
                {
                    //检查图片是否有变
                    string[] fileArr = model.img_urls.Split(',');
                    if (fileArr[0] != txtImgUrl.Text.Trim())
                    {
                        //删除缩略图
                        FileHelper.DeleteFile(model.img_url);
                        //生成新的缩略图
                        model.img_url = "/uploadfiles/thumb/" + DateTime.Now.ToFileTime().ToString() + ".jpg";
                        ImageHelper.CompressImage(txtImgUrl.Text.Trim(), model.img_url, channel.img_width, channel.img_height, "max");
                    }
                }
                else
                {
                    model.img_url = txtImgUrl.Text.Trim();
                }
            }

            else
            {
                //删除缩略图
                FileHelper.DeleteFile(model.img_url);
                //删除图片集合
                if (!string.IsNullOrEmpty(model.img_urls))
                {
                    string[] fileArr = model.img_urls.Split(',');
                    foreach (string fileTemp in fileArr)
                    {
                        FileHelper.DeleteFile(fileTemp);
                    }
                }
                model.img_url = "";
            }

            model.img_urls = txtImgUrl.Text.Trim();
            model.seo_keywords = txtSeoKeywords.Text.Trim();
            //自动提取SEO描述
            if (txtSeoDescription.Text.Trim() != string.Empty)
            {
                model.seo_desc = txtSeoDescription.Text;
            }
            else
            {
                model.seo_desc = He.Common.StringHelper.ClearHtmlTags(txtContent.Value, 250);
            }

            model.content = txtContent.Value;
            model.link_url = txtLinkUrl.Text.Trim();
            model.is_red = chkRed.Checked ? 1 : 0;
            model.is_hot = chkHot.Checked ? 1 : 0;
            model.is_lock = chkLock.Checked ? 1 : 0;
            model.click = int.Parse(txtClick.Text.Trim());
            model.sort_id = Convert.ToInt32(txtSortId.Text.Trim());
            if (bll.Update(model))
            {
                SaveLogs(string.Format("[{0}管理]修改内容:{1}", channel.manage_title, model.title));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！",LastListUrl);
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", Request.RawUrl);
            }
        }
    }
}